package controllers;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import controllers.Application.Login;
import play.api.libs.ws.*;
import play.api.libs.ws.WS.WSRequestHolder;
import play.data.Form;
import scala.concurrent.Future;
import play.mvc.*;



public class FeatureController extends Controller {

	public static JsonNode getfromDB(String link){
		
		String json="";
		 try {
		        URL u = new URL(link);
		        HttpURLConnection c = (HttpURLConnection) u.openConnection();
		        c.setRequestMethod("GET");
		        c.setRequestProperty("Content-length", "0");
		        c.setUseCaches(false);
		        c.setAllowUserInteraction(false);
		        c.connect();
		        int status = c.getResponseCode();

		        switch (status) {
		            case 200:
		            case 201:
		                BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
		                StringBuilder sb = new StringBuilder();
		                String line;
		                while ((line = br.readLine()) != null) {
		                    sb.append(line+"\n");
		                }
		                br.close();
		                json= sb.toString();
		        }

		    } catch (MalformedURLException ex) {
		        
		    } catch (IOException ex) {
		        
		    }
		 ObjectMapper mapper = new ObjectMapper();
		 JsonNode jsonObj=null;

		try {
			jsonObj = mapper.readTree(json);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		return jsonObj;
		
	}
	
	//TEST CONNECTION TO MOVIEDB - TO BE REMOVED
	public static Result connectMovieDB()
	{
		FeatureExtraction.fillDatabase();
		return ok();
		/*
		String json="";
		 try {
		        URL u = new URL("https://api.themoviedb.org/3/movie/550?api_key=87e2038b0b713a173ad4a94537fe4dd8");
		        HttpURLConnection c = (HttpURLConnection) u.openConnection();
		        c.setRequestMethod("GET");
		        c.setRequestProperty("Content-length", "0");
		        c.setUseCaches(false);
		        c.setAllowUserInteraction(false);
		        c.connect();
		        int status = c.getResponseCode();

		        switch (status) {
		            case 200:
		            case 201:
		                BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
		                StringBuilder sb = new StringBuilder();
		                String line;
		                while ((line = br.readLine()) != null) {
		                    sb.append(line+"\n");
		                }
		                br.close();
		                json= sb.toString();
		        }

		    } catch (MalformedURLException ex) {
		        
		    } catch (IOException ex) {
		        
		    }
		 ObjectMapper mapper = new ObjectMapper();
		 JsonNode jsonObj=null;
		 String name="";
		 
		try {
			jsonObj = mapper.readTree(json);
			name = jsonObj.findPath("adult").asText();
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		System.out.println("resrserser "+jsonObj+"  "+name);
		return ok(name
	    	        
		    	    );
	*/
	}
	
	public static Result getMovieLegth(String id){
		
		String link="https://api.themoviedb.org/3/movie/"+id+"?api_key=87e2038b0b713a173ad4a94537fe4dd8";
		JsonNode result=getfromDB(link);
		String length = result.findPath("runtime").asText();
		return ok(length);
		
	}
	
	public static Result IsForAdults(String id){
		
		String link="https://api.themoviedb.org/3/movie/"+id+"?api_key=87e2038b0b713a173ad4a94537fe4dd8";
		JsonNode result=getfromDB(link);
		String adult = result.findPath("adult").asText();
		return ok(adult);
		
	}
	
public static Result getMovieBudget(String id){
		
		String link="https://api.themoviedb.org/3/movie/"+id+"?api_key=87e2038b0b713a173ad4a94537fe4dd8";
		JsonNode result=getfromDB(link);
		String budget = result.findPath("budget").asText();
		return ok(budget);
		
	}

public static Result getMovieGenres(String id){
	
	String link="https://api.themoviedb.org/3/movie/"+id+"?api_key=87e2038b0b713a173ad4a94537fe4dd8";
	JsonNode result=getfromDB(link);
	Iterator<JsonNode> iter=result.findPath("genres").elements();
	List<String> genres=new ArrayList<>();
	while(iter.hasNext()){
		genres.add(iter.next().findPath("name").asText());

	}

	return ok(genres.toString());
	
}

public static Result getPopularity(String id){
	
	String link="https://api.themoviedb.org/3/movie/"+id+"?api_key=87e2038b0b713a173ad4a94537fe4dd8";
	JsonNode result=getfromDB(link);
	String popularity = result.findPath("popularity").asText();
	return ok(popularity);
	
}
	
public static Result getVoteAvg(String id){
	
	String link="https://api.themoviedb.org/3/movie/"+id+"?api_key=87e2038b0b713a173ad4a94537fe4dd8";
	JsonNode result=getfromDB(link);
	String vote = result.findPath("vote_average").asText();
	return ok(vote);
	
}

public static Result getRevenue(String id){
	
	String link="https://api.themoviedb.org/3/movie/"+id+"?api_key=87e2038b0b713a173ad4a94537fe4dd8";
	JsonNode result=getfromDB(link);
	String rev = result.findPath("revenue").asText();
	return ok(rev);
	
}

public static Result getReleaseDate(String id){
	
	String link="https://api.themoviedb.org/3/movie/"+id+"?api_key=87e2038b0b713a173ad4a94537fe4dd8";
	JsonNode result=getfromDB(link);
	String rel = result.findPath("release_date").asText();
	return ok(rel);
	
}
	
	
public static Result getProductionCompanies(String id){
	
	String link="https://api.themoviedb.org/3/movie/"+id+"?api_key=87e2038b0b713a173ad4a94537fe4dd8";
	JsonNode result=getfromDB(link);
	Iterator<JsonNode> iter=result.findPath("production_companies").elements();
	List<String> comp=new ArrayList<>();
	while(iter.hasNext()){
		comp.add(iter.next().findPath("name").asText());

	}

	return ok(comp.toString());
	
}

public static Result getProductionCountries(String id){
	
	String link="https://api.themoviedb.org/3/movie/"+id+"?api_key=87e2038b0b713a173ad4a94537fe4dd8";
	JsonNode result=getfromDB(link);
	Iterator<JsonNode> iter=result.findPath("production_countries").elements();
	List<String> countries=new ArrayList<>();
	while(iter.hasNext()){
		countries.add(iter.next().findPath("name").asText());

	}

	return ok(countries.toString());
	
}

public static Result getCredits(String id){
	
	String link="https://api.themoviedb.org/3/movie/"+id+"/credits?api_key=87e2038b0b713a173ad4a94537fe4dd8";
	JsonNode result=getfromDB(link);
	System.out.println("resrserser "+result);
	//String rev = result.findPath("revenue").asText();
	return ok(link);
	
}
	

public static Result getCast(String id){
	
	String link="https://api.themoviedb.org/3/movie/"+id+"/credits?api_key=87e2038b0b713a173ad4a94537fe4dd8";
	JsonNode result=getfromDB(link);
	Iterator<JsonNode> iter=result.findPath("cast").elements();
	List<String> cast=new ArrayList<>();
	while(iter.hasNext()){
		cast.add(iter.next().findPath("name").asText());

	}
	return ok(cast.toString());
	
}

public static Result getDirector(String id){
	
	String link="https://api.themoviedb.org/3/movie/"+id+"/credits?api_key=87e2038b0b713a173ad4a94537fe4dd8";
	JsonNode result=getfromDB(link);
	Iterator<JsonNode> iter=result.findPath("crew").elements();
	//List<String> cast=new ArrayList<>();
	while(iter.hasNext()){
		JsonNode node=iter.next();
		if(node.findPath("job").asText().equals("Director"));
		return ok(node.findPath("name").asText());

	
	}
	return ok();
	
}

	
}
